package managedbean.itinerary;

import entity.ItinerarySlotEntity;
import exception.ExistException;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.PageRedirectorBean;
import org.primefaces.model.MenuModel;
import session.stateless.ItinerarySessionBean;
import session.stateless.SystemMessagesBean;
import util.entityhelper.DailyItineraryState;
import util.entityhelper.ItinerarySlotState;
import util.generator.TouristMenuGenerator;
import util.localization.WebLocalization;

/**
 *
 * @author Swee Zhi
 */
@ManagedBean
@RequestScoped
public class ItineraryViewDailyItineraryManagedBean implements Serializable {

    @EJB
    private ItinerarySessionBean itineraryMgr;
    // EXCEPTION
    @EJB
    private SystemMessagesBean systemMessages;
    
    // fields for this bean
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    private PageRedirectorBean redirect;
    
    // use entity helpers to extract data (attribute extraction and formatting instead of entity in this open bean)
    private DailyItineraryState dailyItineraryState;
    
    // fields passed from session
    private Long touristAccountId;
    private String preferredLocale;
    private String statusMessageFromEdit;
    private String itineraryType;
    private Long selectedDailyItineraryId;
    private Long selectedItinerarySlotId;
    
    // other fields from database (+ display)
    private Integer dayNo;
    private String dayDate;
    private Collection<ItinerarySlotState> itinerarySlotList;
    
    // display only
    private MenuModel touristMainMenuModel;
    private MenuModel touristNavMenuModel;
    
    public ItineraryViewDailyItineraryManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
        
        redirect = new PageRedirectorBean();
        /* pre-load values from session and database */
        // get account ID values from session
        if (ctx.getExternalContext().getSessionMap().containsKey("touristAccountId")) {
            touristAccountId = (Long) ctx.getExternalContext().getSessionMap().get("touristAccountId");
            // put back the session IDs taken
            ctx.getExternalContext().getSessionMap().put("touristAccountId", touristAccountId);
            
            if (ctx.getExternalContext().getSessionMap().containsKey("statusMessageFromEdit")) {
                statusMessageFromEdit = (String) ctx.getExternalContext().getSessionMap().get("statusMessageFromEdit");
                ctx.getExternalContext().getSessionMap().remove("statusMessageFromEdit");
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("itineraryType")) {
                itineraryType = (String) ctx.getExternalContext().getSessionMap().get("itineraryType");
                ctx.getExternalContext().getSessionMap().put("itineraryType", itineraryType);
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("selectedDailyItineraryId")) {
                selectedDailyItineraryId = (Long) ctx.getExternalContext().getSessionMap().get("selectedDailyItineraryId");
                ctx.getExternalContext().getSessionMap().put("selectedDailyItineraryId", selectedDailyItineraryId);
            }
            
            if (ctx.getExternalContext().getSessionMap().containsKey("selectedItinerarySlotId")) {
                selectedItinerarySlotId = (Long) ctx.getExternalContext().getSessionMap().get("selectedItinerarySlotId");
                ctx.getExternalContext().getSessionMap().put("selectedItinerarySlotId", selectedItinerarySlotId);
            }
        }
        else {
            // redirect back to login
            redirect.goTouristLoginPageAtPreviousLevel();
        }
        /* END load values from session and database */    
    }
    
    public void initView(PhaseEvent event) {
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        try {
            if (statusMessageFromEdit != null) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessageFromEdit, ""));
            }
            itineraryMgr.setSelectedLocale(preferredLocale);
            
            try {
                dailyItineraryState = itineraryMgr.getDailyItineraryByIDReturnByDailyItineraryState(selectedDailyItineraryId);
            }
            catch (ExistException ex) {
//                System.out.println(ex.getMessage());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
            }
            
            dayNo = dailyItineraryState.getDayNo();
            dayDate = localization.getLocalizedDateFormat(dailyItineraryState.getDayDate());
            
            if (itinerarySlotList == null) {
                try {
                    itinerarySlotList = itineraryMgr.getItinerarySlotsByDailyItineraryIDReturnByItinerarySlotState(selectedDailyItineraryId);
                    System.out.println(itinerarySlotList.size());
                    System.out.println(itinerarySlotList.iterator().next().getLocalizedActivityName());
                    System.out.println(itinerarySlotList.iterator().next().getLocalizedActivityType());
                    
                }
                catch (ExistException ex) {
//                    System.out.println(ex.getMessage());
//                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getLocalizedMessage(), ""));
                }
            }
            
            // main menu 
            TouristMenuGenerator menuGeneratorMain = new TouristMenuGenerator();
            touristMainMenuModel = menuGeneratorMain.generateTouristMainMenu();
            // nav menu
            TouristMenuGenerator menuGeneratorNav = new TouristMenuGenerator();
            touristNavMenuModel = menuGeneratorNav.generateTouristNavMenu();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void editItineraryActivities() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedDailyItineraryId", selectedDailyItineraryId);
        if (itinerarySlotList != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("storedInSessionItinerarySlotListFromViewDaily", itinerarySlotList);
        }
        redirect.goItineraryEditDailyItineraryActivitiesPage();
//        FacesContext ctx = FacesContext.getCurrentInstance();
//        String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/itinerary/ItineraryViewActivityDetails.xhtml"));
//        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        
        //TODO: edit itinerary slot page

    }
    
    public void viewItinerarySlotView(ItinerarySlotEntity itinerarySlot) throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedItinerarySlotId", itinerarySlot.getId());
//        redirect.goItineraryViewTripItineraryPage();
        FacesContext ctx = FacesContext.getCurrentInstance();
        String nextPage = FacesContext.getCurrentInstance().getExternalContext().encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/itinerary/ItineraryViewActivityDetails.xhtml"));
        FacesContext.getCurrentInstance().getExternalContext().redirect(nextPage);
        
        //TODO: edit itinerary slot page

    }

    public void viewItinerarySlotDelete(ItinerarySlotEntity itinerarySlot) throws IOException {
        //TODO: delete itinerary slot page
        FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedItinerarySlotId", itinerarySlot.getId());
//        redirect.goDeleteTripItineraryPage();
    }

    public FacesContext getCtx() {
        return ctx;
    }

    public void setCtx(FacesContext ctx) {
        this.ctx = ctx;
    }

    public DailyItineraryState getDailyItineraryState() {
        return dailyItineraryState;
    }

    public void setDailyItineraryState(DailyItineraryState dailyItineraryState) {
        this.dailyItineraryState = dailyItineraryState;
    }

    public String getDayDate() {
        return dayDate;
    }

    public void setDayDate(String dayDate) {
        this.dayDate = dayDate;
    }

    public Integer getDayNo() {
        return dayNo;
    }

    public void setDayNo(Integer dayNo) {
        this.dayNo = dayNo;
    }

    public ItinerarySessionBean getItineraryMgr() {
        return itineraryMgr;
    }

    public void setItineraryMgr(ItinerarySessionBean itineraryMgr) {
        this.itineraryMgr = itineraryMgr;
    }

    public String getItineraryType() {
        return itineraryType;
    }

    public void setItineraryType(String itineraryType) {
        this.itineraryType = itineraryType;
    }

    public WebLocalization getLocalization() {
        return localization;
    }

    public void setLocalization(WebLocalization localization) {
        this.localization = localization;
    }

    public String getPreferredLocale() {
        return preferredLocale;
    }

    public void setPreferredLocale(String preferredLocale) {
        this.preferredLocale = preferredLocale;
    }

    public PageRedirectorBean getRedirect() {
        return redirect;
    }

    public void setRedirect(PageRedirectorBean redirect) {
        this.redirect = redirect;
    }

    public String getStatusMessageFromEdit() {
        return statusMessageFromEdit;
    }

    public void setStatusMessageFromEdit(String statusMessageFromEdit) {
        this.statusMessageFromEdit = statusMessageFromEdit;
    }

    public SystemMessagesBean getSystemMessages() {
        return systemMessages;
    }

    public void setSystemMessages(SystemMessagesBean systemMessages) {
        this.systemMessages = systemMessages;
    }

    public Long getTouristAccountId() {
        return touristAccountId;
    }

    public void setTouristAccountId(Long touristAccountId) {
        this.touristAccountId = touristAccountId;
    }

    public MenuModel getTouristMainMenuModel() {
        return touristMainMenuModel;
    }

    public void setTouristMainMenuModel(MenuModel touristMainMenuModel) {
        this.touristMainMenuModel = touristMainMenuModel;
    }

    public MenuModel getTouristNavMenuModel() {
        return touristNavMenuModel;
    }

    public void setTouristNavMenuModel(MenuModel touristNavMenuModel) {
        this.touristNavMenuModel = touristNavMenuModel;
    }

    public Long getSelectedDailyItineraryId() {
        return selectedDailyItineraryId;
    }

    public void setSelectedDailyItineraryId(Long selectedDailyItineraryId) {
        this.selectedDailyItineraryId = selectedDailyItineraryId;
    }

    public Long getSelectedItinerarySlotId() {
        return selectedItinerarySlotId;
    }

    public void setSelectedItinerarySlotId(Long selectedItinerarySlotId) {
        this.selectedItinerarySlotId = selectedItinerarySlotId;
    }

    public Collection<ItinerarySlotState> getItinerarySlotList() {
        return itinerarySlotList;
    }

    public void setItinerarySlotList(Collection<ItinerarySlotState> itinerarySlotList) {
        this.itinerarySlotList = itinerarySlotList;
    }

}
